﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Web.Controllers
{
    public class AddressController : Controller
    {
        //
        // GET: /Address/

        public ActionResult Index(AddressSendModle Model)
        {
            if (Model.page == 0)
                Model.page = 1;
            if (Model.key == null)
                Model.key = "";
            WebConfigProvider configProvider = new WebConfigProvider();
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            int max = Convert.ToInt32(configProvider.MaxshowInHistory);
            //TODO UserID必须得到当前用户
            Model.Infos = dataAccess.GetNumberByUser(2,Model.page,max,Model.key);
            Model.maxpages = dataAccess.GetPageCount(2,max,Model.key);
            return View(Model);
        }

        public ActionResult AddAddress(string name, string number)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            if (name == "")
            {
                TempData["msg"] = "请输入联系人姓名";
                return Redirect("index");
            }
            else if (number == "")
            {
                TempData["msg"] = "请输入联系人号码";
                return Redirect("index");
            }
            else
            {
                int result = dataAccess.AddAddress(name, number);
                if (result != 0)
                {
                    TempData["msg"] = "联系人添加成功";
                    return Redirect("index");
                }
                else
                {
                    TempData["msg"] = "联系人添加失败";
                    return Redirect("index");
                }
            }
        }

        public ActionResult deleteAddress(int addressid) 
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            if (addressid.ToString().Length != 0)
            {
                dataAccess.DeleteNumberByAddressId(addressid);
            }
            AddressSendModle Model = new AddressSendModle();
            if (Model.page == 0)
                Model.page = 1;
            if (Model.key == null)
                Model.key = "";
            int max = Convert.ToInt32(configProvider.MaxshowInHistory);
            //TODO UserID必须得到当前用户
            Model.Infos = dataAccess.GetNumberByUser(2, Model.page, max, Model.key);
            Model.maxpages = dataAccess.GetPageCount(2, max, Model.key);
            return View("Address_Book",Model);
        }

        public ActionResult deleteAddressByItem(string[] items)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            if (items != null && items.Length != 0)
            {
                foreach (var id in items)
                    { dataAccess.DeleteNumberByAddressId(Convert.ToInt32(id));
                }
            }
            AddressSendModle Model = new AddressSendModle();
            if (Model.page == 0)
                Model.page = 1;
            if (Model.key == null)
                Model.key = "";
            int max = Convert.ToInt32(configProvider.MaxshowInHistory);
            //TODO UserID必须得到当前用户
            Model.Infos = dataAccess.GetNumberByUser(2, Model.page, max, Model.key);
            Model.maxpages = dataAccess.GetPageCount(2, max, Model.key);
            return View("Address_Book", Model);
        }

        public ActionResult findByKey(AddressSendModle Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            //TODO UserID必须得到当前用户
            Model.Infos = dataAccess.GetNumberByText(Model.key,2);
            return View("index",Model);
        }
    }
}
